//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.carhailing.system.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.carhailing.common.requst.VerifyReq;
import com.carhailing.common.response.IdentificationResponse;
import com.carhailing.common.response.LiveResponse;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component
public class OssUtil {
    @Value("${aliyun.oss.endpoint}")
    public String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    public String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    public String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    public String bucketName;
    @Value("${aliyun.oss.objectName}")
    public String objectName;
    @Value("${aliyun.appcode}")
    public String appcode;
    @Value("${aliyun.liveUrl}")
    public String liveUrl;
    @Value("${aliyun.img}")
    public String img;
    public static final String IDDENTITYURL = "https://jumdata.market.alicloudapi.com/idcard/validate";
    public static final String OCRURL = "https://cardnumber.market.alicloudapi.com/rest/160601/ocr/ocr_idcard.json";

    public OssUtil() {
    }

    public String fileUpload(MultipartFile multipartFile) throws Exception {
        InputStream inputStream = multipartFile.getInputStream();
        String originalFilename = multipartFile.getOriginalFilename();
        OSS ossClient = (new OSSClientBuilder()).build(this.endpoint, this.accessKeyId, this.accessKeySecret);
        PutObjectRequest request = new PutObjectRequest(this.bucketName, this.objectName + originalFilename, inputStream);
        ossClient.putObject(request);
        Date date = new Date(System.currentTimeMillis() + 604800000L);
        return ossClient.generatePresignedUrl(this.bucketName, this.objectName + originalFilename, date).toString();
    }

    public VerifyReq identity(String idCardNo, String name) {
        String url = "https://jumdata.market.alicloudapi.com/idcard/validate";
        Map map = new HashMap();
        map.put("name", name);
        map.put("idCardNo", idCardNo);
        HttpResponse response = ((HttpRequest)HttpUtil.createPost(url).header("Authorization", "APPCODE " + this.appcode)).form(map).execute();
        String body = response.body();
        VerifyReq Verify = (VerifyReq) JSONObject.parseObject(body, VerifyReq.class);
        return Verify;
    }

    public LiveResponse checkLife(String liveUrl) {
        Map map = new HashMap();
        String url = "https://life.shumaidata.com/checklife";
        map.put("url", liveUrl);
        map.put("motions", "NOD");
        String authorization = ((HttpRequest)HttpUtil.createPost(url).header("Authorization", "APPCODE " + this.appcode)).form(map).execute().body();
        LiveResponse liveResponse = (LiveResponse)JSONObject.parseObject(authorization, LiveResponse.class);
        return liveResponse;
    }

    public IdentificationResponse backMessage(String img) {
        String url = "https://cardnumber.market.alicloudapi.com/rest/160601/ocr/ocr_idcard.json";
        JSONObject configObj = new JSONObject();
        configObj.put("side", "face");
        configObj.put("quality_info", false);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("image", img);
        jsonObject.put("configure", configObj);
        HttpResponse response = ((HttpRequest)((HttpRequest)HttpUtil.createPost(url).header("Authorization", "APPCODE " + this.appcode)).header("Content-Type", "application/json; charset=UTF-8")).body(JSON.toJSONString(jsonObject)).execute();
        if (response != null) {
            String str = response.body();
            IdentificationResponse ident = (IdentificationResponse)JSONObject.parseObject(str, IdentificationResponse.class);
            ident.setUrl(img);
            return ident;
        } else {
            return null;
        }
    }
}
